# *_*coding:utf-8 *_*
import logging

from downloader.base import Base

logger = logging.getLogger('downloader')
logger.addHandler(logging.NullHandler())


class SingleDownloader(Base):
    def __init__(self, *args, **kwargs):
        super(SingleDownloader, self).__init__(*args, **kwargs)

    def single_download(self, urlinfo):
        logger.debug(f"get download info: {urlinfo}")
        url, dl_path, callback = urlinfo
        res = self._get(url)
        if res.status_code != 200:
            logger.error(f"status_code:{res.status_code} err! text:{res.text}")
            return
        with open(dl_path, 'wb') as fp:
            fp.write(res.content)
        callback_result = None
        if callable(callback):
            callback_result = callback()
        return f"{url} {dl_path} {callback_result}"

    def add(self, url, filepath, callback=None, *args, **kwargs):
        self.single_download([url, filepath, callback])


if __name__ == '__main__':
    s = SingleDownloader()
    s.add("", "./1.ts")
